using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankWars
{
    public class StatusArea
    {
        private Texture2D borderTexture;
        private SpriteFont font;
        private Tank playerTank;

        private Color cFontColor = Color.Black;
        private Color cBackgroundColor = Color.Beige;

        private Viewport StatusAreaViewport;

        private int nHeight = 0;
        private int nWidth = 0;
        private int nXPos = 0;
        private int nYPos = 0;

        public StatusArea(Texture2D borderTexture, SpriteFont font, Tank playerTank, Viewport viewport)
        {
            this.borderTexture = borderTexture;
            this.playerTank = playerTank;

            this.font = font;

            this.GetSize(viewport);
        }

        /// <summary>
        /// Sets the size of the status area based on the given viewport and creates a new viewport of that size
        /// </summary>
        /// <param name="viewport"></param>
        private void GetSize(Viewport viewport)
        {
            this.nHeight = this.borderTexture.Height;
            this.nWidth = viewport.Width;

            this.nXPos = 0;
            this.nYPos = viewport.Height - this.borderTexture.Height;

            this.StatusAreaViewport = viewport;
            this.StatusAreaViewport.Width = this.nWidth;
            this.StatusAreaViewport.Height = this.nHeight;
            this.StatusAreaViewport.X = this.nXPos;
            this.StatusAreaViewport.Y = this.nYPos;
        }

        /// <summary>
        /// Resizes the input viewport to not include the status area and returns the new viewport
        /// </summary>
        /// <param name="viewport">Viewport to resize</param>
        /// <returns>Resized viewport</returns>
        public Viewport ResizeViewport(Viewport viewport)
        {
            viewport.Height -= this.nHeight;
            return viewport;
        }

        /// <summary>
        /// Draws all stats in the status area
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="graphicsDevice"></param>
        public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Microsoft.Xna.Framework.Input.MouseState mousestate)
        {
            Viewport tempViewport = graphicsDevice.Viewport;

            graphicsDevice.Viewport = this.StatusAreaViewport;
            graphicsDevice.Clear(this.cBackgroundColor);
            spriteBatch.Begin();

            //Draw border
            spriteBatch.Draw(this.borderTexture, new Rectangle(0, 0, this.nWidth, this.nHeight), Color.White);
            this.DrawStats(spriteBatch, mousestate);

            spriteBatch.End();

            graphicsDevice.Viewport = tempViewport;
        }

        /// <summary>
        /// Draws each stat in the apporpiate area
        /// </summary>
        /// <param name="spriteBatch"></param>
        private void DrawStats(SpriteBatch spriteBatch, Microsoft.Xna.Framework.Input.MouseState mousestate)
        {
            #region Get strings to draw
            string strPlayerName = this.playerTank.TankPlayer.Name;
            Vector2 vNamePos = new Vector2(10, 10);
            Vector2 vNameSize = this.font.MeasureString(strPlayerName);

            string strHealthString = "Health :";
            Vector2 vHealthStringPos = new Vector2(vNameSize.X + vNamePos.X + 30, vNamePos.Y);
            Vector2 vHealthStringSize = this.font.MeasureString(strHealthString);

            string strHealthValue = this.playerTank.Health.ToString();
            Vector2 vHealthValuePos = new Vector2(vHealthStringSize.X + vHealthStringPos.X + 5, vHealthStringPos.Y);
            Vector2 vHealthValueSize = this.font.MeasureString(strHealthValue);

            string strKillsString = "Kills :";
            Vector2 vKillsStringPos = new Vector2(vHealthValueSize.X + vHealthValuePos.X + 30, 10);
            Vector2 vKillsStringSize = this.font.MeasureString(strKillsString);

            string strKillsValue = "Mouse - (" + mousestate.X.ToString() + "," + mousestate.Y.ToString() + ")" + " Tank - (" + MathHelper.ToDegrees(this.playerTank.Turret.fRotation).ToString() + "," + this.playerTank.vPosition.Y + ")";//this.playerTank.TankPlayer.Kills.ToString();
            Vector2 vKillsValuePos = new Vector2(vKillsStringSize.X + vKillsStringPos.X + 5, vKillsStringPos.Y);
            Vector2 vKillsValueSize = this.font.MeasureString(strKillsValue);

            string strPrimaryWeaponString = "Primary Weapon :";
            Vector2 vPrimaryWeaponStringPos = new Vector2(vNamePos.X, vNamePos.Y + vNameSize.Y + 10);
            Vector2 vPrimaryWeaponStringSize = this.font.MeasureString(strPrimaryWeaponString);

            Texture2D PrimaryWeaponIcon = this.playerTank.WeaponManager.ShotIcons[this.playerTank.WeaponManager.PrimaryShotType];
            Vector2 vPrimaryWeaponIconPos = new Vector2(vPrimaryWeaponStringSize.X + vPrimaryWeaponStringPos.X + 5, vPrimaryWeaponStringPos.Y);
            float fIconHeight = vPrimaryWeaponStringSize.Y;
            float fNewIconSizeRatio = fIconHeight / PrimaryWeaponIcon.Height;
            float fIconWidth = PrimaryWeaponIcon.Width * fNewIconSizeRatio;
            Vector2 vPrimaryWeaponIconSize = new Vector2(fIconWidth, fIconHeight);

            string strPrimaryShotAmmoEnergy = "";
            System.Reflection.FieldInfo fieldInfo;
            if (this.playerTank.WeaponManager.PrimaryWeaponCategory == TankWars.Weapons.WeaponManager.WeaponCategory.Ammo)
            {
                fieldInfo = this.playerTank.WeaponManager.PrimaryShotType.GetField("N_MAX_AMMO");
                strPrimaryShotAmmoEnergy = this.playerTank.WeaponManager.PrimaryAmmo.ToString() + "/" + ((int)fieldInfo.GetValue(null)).ToString();
            }
            else if (this.playerTank.WeaponManager.PrimaryWeaponCategory == TankWars.Weapons.WeaponManager.WeaponCategory.Energy)
            {
                //No max value
                strPrimaryShotAmmoEnergy = this.playerTank.WeaponManager.PrimaryEnergy.ToString() + "/100";
            }
            else
            {
                strPrimaryShotAmmoEnergy = "Infinite";
                //strPrimaryShotAmmoEnergy = Convert.ToChar(236).ToString() + "/" + Convert.ToChar(236).ToString();
            }
            Vector2 vPrimaryShotAmmoEnergyPos = new Vector2(vPrimaryWeaponIconSize.X + vPrimaryWeaponIconPos.X + 5, vPrimaryWeaponStringPos.Y);
            Vector2 vPrimaryShotAmmoEnergySize = this.font.MeasureString(strPrimaryShotAmmoEnergy);

            string strSecondaryWeaponString = "Secondary Weapon :";
            Vector2 vSecondaryWeaponStringPos = new Vector2(10, vPrimaryWeaponStringPos.Y + vPrimaryWeaponStringSize.Y + 10);
            Vector2 vSecondaryWeaponStringSize = this.font.MeasureString(strSecondaryWeaponString);

            Texture2D SecondaryWeaponIcon = this.playerTank.WeaponManager.ShotIcons[this.playerTank.WeaponManager.SecondaryShotType];
            Vector2 vSecondaryWeaponIconPos = new Vector2(vSecondaryWeaponStringSize.X + vSecondaryWeaponStringPos.X + 5, vSecondaryWeaponStringPos.Y);
            fIconHeight = vSecondaryWeaponStringSize.Y;
            fNewIconSizeRatio = fIconHeight / SecondaryWeaponIcon.Height;
            fIconWidth = SecondaryWeaponIcon.Width * fNewIconSizeRatio;
            Vector2 vSecondaryWeaponIconSize = new Vector2(fIconWidth, fIconHeight);

            string strSecondaryShotAmmoEnergy = "";
            if (this.playerTank.WeaponManager.SecondaryWeaponCategory == TankWars.Weapons.WeaponManager.WeaponCategory.Ammo)
            {
                fieldInfo = this.playerTank.WeaponManager.SecondaryShotType.GetField("N_MAX_AMMO");
                strSecondaryShotAmmoEnergy = this.playerTank.WeaponManager.SecondaryAmmo.ToString() + "/" + ((int)fieldInfo.GetValue(null)).ToString();
            }
            else if (this.playerTank.WeaponManager.SecondaryWeaponCategory == TankWars.Weapons.WeaponManager.WeaponCategory.Energy)
            {
                //No max value
                strSecondaryShotAmmoEnergy = this.playerTank.WeaponManager.SecondaryEnergy.ToString() + "/100";
            }
            else
            {
                strSecondaryShotAmmoEnergy = "Infinite";
                //strPrimaryShotAmmoEnergy = Convert.ToChar(236).ToString() + "/" + Convert.ToChar(236).ToString();
            }
            Vector2 vSecondaryShotAmmoEnergyPos = new Vector2(vSecondaryWeaponIconSize.X + vSecondaryWeaponIconPos.X + 5, vSecondaryWeaponStringPos.Y);
            Vector2 vSecondaryShotAmmoEnergySize = this.font.MeasureString(strSecondaryShotAmmoEnergy);
            #endregion

            #region Draw all strings
            //Draw player name
            spriteBatch.DrawString(this.font, strPlayerName, vNamePos, this.cFontColor);

            //Draw player health
            spriteBatch.DrawString(this.font, strHealthString, vHealthStringPos, this.cFontColor);
            spriteBatch.DrawString(this.font, strHealthValue, vHealthValuePos, this.cFontColor);

            //Draw player kills
            spriteBatch.DrawString(this.font, strKillsString, vKillsStringPos, this.cFontColor);
            spriteBatch.DrawString(this.font, strKillsValue, vKillsValuePos, this.cFontColor);

            //Draw Primary weapon
            spriteBatch.DrawString(this.font, strPrimaryWeaponString, vPrimaryWeaponStringPos, this.cFontColor);
            spriteBatch.Draw(PrimaryWeaponIcon, new Rectangle((int)vPrimaryWeaponIconPos.X, (int)vPrimaryWeaponIconPos.Y, (int)vPrimaryWeaponIconSize.X, (int)vPrimaryWeaponIconSize.Y), Color.White);
            spriteBatch.DrawString(this.font, strPrimaryShotAmmoEnergy, vPrimaryShotAmmoEnergyPos, this.cFontColor);

            //Draw Secondary weapon
            spriteBatch.DrawString(this.font, strSecondaryWeaponString, vSecondaryWeaponStringPos, this.cFontColor);
            spriteBatch.Draw(SecondaryWeaponIcon, new Rectangle((int)vSecondaryWeaponIconPos.X, (int)vSecondaryWeaponIconPos.Y, (int)vSecondaryWeaponIconSize.X, (int)vSecondaryWeaponIconSize.Y), Color.White);
            spriteBatch.DrawString(this.font, strSecondaryShotAmmoEnergy, vSecondaryShotAmmoEnergyPos, this.cFontColor);
            #endregion
        }
    }
}
